#include "stdio.h"
#include "CoordinatesVector.h"
#include <stdlib.h>
#include <ctime>

CoordinatesVector::CoordinatesVector(void)
{
}

CoordinatesVector::CoordinatesVector(unsigned short Size, short MinCoordValue, short MaxCoordValue)
{
	this->Size = Size;
	this->MinCoordValue = MinCoordValue;
	this->MaxCoordValue = MaxCoordValue;
	this->CoordinateTab = new Coordinate[Size];
}

CoordinatesVector::CoordinatesVector(const CoordinatesVector & copy)
{
	this->Size = copy.Size;
	this->MinCoordValue = copy.MinCoordValue;
	this->MaxCoordValue = copy.MaxCoordValue;
	this->Evaluation = copy.Evaluation;
	this->CoordinateTab = new Coordinate[Size];
	for(int i=0; i<Size; ++i)
	{
		this->CoordinateTab[i] = copy.CoordinateTab[i];
	}
}

CoordinatesVector::~CoordinatesVector(void)
{
	if(CoordinateTab != NULL)
	{
		delete[] CoordinateTab;
	}
}

void CoordinatesVector::resetPointer(void)
{
	CoordinateTab = NULL;
}

unsigned short CoordinatesVector::getSize(void)
{
	return Size;
}

Coordinate * CoordinatesVector::getCoordinateTab(void)
{
	return CoordinateTab;
}

void CoordinatesVector::randomInitialize(void)
{
	if(RAND_MAX < 0xffff)
	{
		printf("RAND_MAX is less than 0xffff. EncodedValue is not random number from whole range \n");
	}

	for(int i=0; i<Size; ++i)
	{
		//set encoded value
		unsigned short RandomValue = rand() % 0x010000; //jeśli większe od 0xffff to modulo
		CoordinateTab[i].setEncodedValue(RandomValue);

		//set coordinate value
		double DecodedValue = (double)RandomValue * ((double)(MaxCoordValue - MinCoordValue) / 0xffff) + MinCoordValue;
		CoordinateTab[i].setValue(DecodedValue);
	}
}

void CoordinatesVector::setEvaluation(double Evaluation)
{
	this->Evaluation = Evaluation;
}

double CoordinatesVector::getEvaluation(void)
{
	return Evaluation;
}

unsigned short CoordinatesVector::getCoordinateEncodedValue(int coordinateNumber)
{
	return CoordinateTab[coordinateNumber].getEncodedValue();
}

void CoordinatesVector::initialize(unsigned short encodedValueTab[])
{
	if(RAND_MAX < 0xffff)
	{
		printf("RAND_MAX is less than 0xffff. EncodedValue is not random number from whole range \n");
	}

	for(int i=0; i<Size; ++i)
	{
		//set encoded value
		unsigned short Value = encodedValueTab[i];
		CoordinateTab[i].setEncodedValue(Value);

		//set coordinate value
		double DecodedValue = (double)Value * ((double)(MaxCoordValue - MinCoordValue) / 0xffff) + MinCoordValue;
		CoordinateTab[i].setValue(DecodedValue);
	}
}
